package attack.yefi;

import java.awt.geom.Rectangle2D;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Rectangle;

import framework.BasicFighter;
import framework.Util;
import attack.nartan.FireBall;

public class SwordStorm extends FireBall {
	
	private int lastHit;
	
	final private static int WIND_UP = 180;
	
	public SwordStorm(BasicFighter f) {
		super(f);
		Image[] frames = new Image[8];
		frames[0] = this.spriteSheet.getSprite(0, 2);
		frames[1] = this.spriteSheet.getSprite(2, 2);
		frames[2] = this.spriteSheet.getSprite(3, 2);
		frames[3] = this.spriteSheet.getSprite(2, 2);
		frames[4] = this.spriteSheet.getSprite(3, 2);
		frames[5] = this.spriteSheet.getSprite(2, 2);
		frames[6] = this.spriteSheet.getSprite(1, 2);
		frames[7] = this.spriteSheet.getSprite(1, 2);
		this.attackAnimation = new Animation(frames, 40);
		this.attackAnimation.setDuration(7, 800);
		this.attackAnimationBackward = Util.getFlippedAnimation(this.attackAnimation, true, false);
		this.attackAnimation.setLooping(false);
		this.attackAnimationBackward.setLooping(false);
		frames[0] = this.spriteSheet.getSprite(4, 2);
		frames[1] = this.spriteSheet.getSprite(0, 3);
		frames[2] = this.spriteSheet.getSprite(1, 3);
		frames[3] = this.spriteSheet.getSprite(0, 3);
		frames[4] = this.spriteSheet.getSprite(1, 3);
		frames[5] = this.spriteSheet.getSprite(0, 3);
		frames[6] = this.spriteSheet.getSprite(2, 3);
		frames[7] = this.spriteSheet.getSprite(2, 3);
		this.attackJumpAnimation = new Animation(frames, 40);
		this.attackJumpAnimation.setDuration(7, 800);
		this.attackJumpAnimationBackward = Util.getFlippedAnimation(this.attackJumpAnimation, true, false);
		this.attackJumpAnimation.setLooping(false);
		this.attackJumpAnimationBackward.setLooping(false);
		
		frames = new Image[3];
		frames[0] = this.spriteSheet.getSprite(3, 3);
		frames[1] = this.spriteSheet.getSprite(4, 3);
		frames[2] = this.spriteSheet.getSprite(0, 4);
		this.ball = new Animation(frames, 50);
		this.ballBackward = Util.getFlippedAnimation(this.ball, true, false);
		
		this.interruptOnHit = false;
	}

	@Override
	public void trigger() {
		super.trigger();
		lastHit = 0;
		setXspeed(0);
		if (getAttacker().isForward()) {
			setX((int) (getAttacker().getHitBox().getX() - getAttacker().getHitBox().getWidth() * 3.5));
		}
	}

	@Override
	public void update(GameContainer gc, int delta) {
		super.update(gc, delta);
		// fixing the move while attacking bug
		if (getAttacker().isForward()) {
			setX((int) (getAttacker().getHitBox().getX() - getAttacker().getHitBox().getWidth() * 3.5));
		} else {
			setX((int) (getAttacker().getHitBox().getX() - getAttacker().getHitBox().getWidth() * 4.5));
		}	
		setY((int) (getAttacker().getY()));
	}
	
	

	@Override
	public int getDuration() {
		return Util.getDuration(attackAnimation);
	}
	
	@Override
	public int getPower() {
		return 5;
	}

	@Override
	public void onRelease() {
		interrupt();
	}

	@Override
	public boolean isBlocking() {
		return isActive();
	}
	
	@Override
	public boolean collidesOnAttack() {
		return false;
	}
	
	@Override
	public Rectangle getHitBox() {
		int offset = getWidth() / 6;
		int width = (int) (offset * 2.5);
		Rectangle h = getAttacker().getHitBox();
		Rectangle hitBox;
		hitBox = null;
		if (completed) {
			if (this.currentBall == this.ball) {
				hitBox = new Rectangle(h.getX() + h.getWidth(), getY() + offset, width, offset);	
			} else {
				hitBox = new Rectangle(h.getX() - width, getY() + offset, width, offset);	
			}
		} 
		return hitBox;
	}

	@Override
	public void onHit(BasicFighter f, Rectangle2D collision) {
		if (f.getCurrentAttack().isInterruptable()) {
		    f.getCurrentAttack().interrupt();
		}
		if (lastHit + WIND_UP < currentTime) {
			if (f.isParrying()) {
				f.parry(getPower());
			} else {
				f.hit(getPower());
				this.hitSound.stop();	
				this.hitSound.play();
			}
			lastHit = currentTime;
		}
	}
}
